<template>
  <a-radio-group v-if="tagType=='radio'" @change="handleInput" :value="getValueSting" :disabled="disabled">
    <a-radio v-for="(item, key) in dictOptions" :key="key" :value="item.value">{{ item.text }}</a-radio>
  </a-radio-group>

  <a-radio-group v-else-if="tagType=='radioButton'" buttonStyle="solid" @change="handleInput" :value="getValueSting"
                 :disabled="disabled">
    <a-radio-button v-for="(item, key) in dictOptions" :key="key" :value="item.value">{{ item.text }}</a-radio-button>
  </a-radio-group>

  <a-select v-else-if="tagType=='select'" :getPopupContainer="getPopupContainer" :placeholder="placeholder"
            :disabled="disabled" :value="getValueSting" @change="handleInput">
    <a-select-option :value="undefined">请选择</a-select-option>
    <a-select-option v-for="(item, key) in dictOptions" :key="key" :value="item.value">
      <span style="display: inline-block;width: 100%" :title=" item.text || item.label ">
        {{ item.text || item.label }}
      </span>
    </a-select-option>
  </a-select>
</template>

<script>
import { ajaxGetDictItems, getDictItemsFromCache, serviceAjaxGetDictItems, sysAjaxGetDictItems } from '@/api/api'

export default {
  name: "JDictSelectTag",
  props: {
    dictCode: String,
    placeholder: String,
    triggerChange: Boolean,
    disabled: Boolean,
    value: [String, Number],
    type: String,
    getPopupContainer: {
      type: Function,
      default: (node) => node.parentNode
    }
  },
  data() {
    return {
      dictOptions: [],
      tagType: ""
    }
  },
  watch: {
    dictCode: {
      immediate: true,
      handler() {
        this.initDictData()
      },
    }
  },
  created() {
    if (!this.type || this.type === "list") {
      this.tagType = "select"
    } else {
      this.tagType = this.type
    }
    //获取字典数据
    // this.initDictData();
  },
  computed: {
    getValueSting() {
      // update-begin author:wangshuai date:20200601 for: 不显示placeholder的文字 ------
      // 当有null或“” placeholder不显示
      return this.value != null ? this.value.toString() : undefined;
      // update-end author:wangshuai date:20200601 for: 不显示placeholder的文字 ------
    },
  },
  methods: {
    initDictData() {
      //优先从缓存中读取字典配置
      if (getDictItemsFromCache(this.dictCode)) {
        this.dictOptions = getDictItemsFromCache(this.dictCode);
        return
      }

      // --------------------------update-begin author:liujb date:20220330 解决分库找不到表问题----------------------------------
      let dictCode = this.dictCode;
      if (dictCode && dictCode.indexOf(',') != -1) {
        //关联表字典（举例：sys_user,realname,id）
        let dictCodeSplit = this.dictCode.split(","); //以，分割
        let tableName = dictCodeSplit[0];
        let tableNameSplit = tableName.split("_");
        /* if ("hmhb" === tableNameSplit[0]){
           hmhbAjaxGetDictItems(this.dictCode, null).then((res) => {
             if (res.success) {
               this.dictOptions = res.result;
             }
           })
         }else if ("erp" === tableNameSplit[0]){
           erpAjaxGetDictItems(this.dictCode, null).then((res) => {
             if (res.success) {
               this.dictOptions = res.result;
             }
           })
         }else if ("imp" === tableNameSplit[0]){
           impAjaxGetDictItems(this.dictCode, null).then((res) => {
             if (res.success) {
               this.dictOptions = res.result;
             }
           })
         }else{
           sysAjaxGetDictItems(this.dictCode, null).then((res) => {
             if (res.success) {
               this.dictOptions = res.result;
             }
           })
         }*/
        //以上注释代码做如下改动，是为了避免以后增加服务后，还要再修改这块的代码。开闭原则 modified by wuzx 20220601
        if (tableNameSplit && tableNameSplit.length > 0 && tableNameSplit[0] && tableNameSplit[0] != 'sys') {
          serviceAjaxGetDictItems(tableNameSplit[0], dictCode).then((res) => {
            if (res.success) {
              this.dictOptions = res.result;
            }
          });
        } else {
          ajaxGetDictItems(dictCode).then((res) => {
            if (res.success) {
              this.dictOptions = res.result;
            }
          });
        }
      }else{
        //根据字典Code, 初始化字典数组
        sysAjaxGetDictItems(this.dictCode, null).then((res) => {
          if (res.success) {
            this.dictOptions = res.result;
          }
        })
      }

// --------------------------update-end author:liujb date:20220330----------------------------------
      //根据字典Code, 初始化字典数组
      /* sysAjaxGetDictItems(this.dictCode, null).then((res) => {
         if (res.success) {
           this.dictOptions = res.result;
         }
       })*/

    },
    handleInput(e) {
      let val;
      if (this.tagType == "radio") {
        val = e.target.value
      } else {
        val = e
      }

      var name = val?this.dictOptions.filter(v => v.value === val)[0].text:''
      if (this.triggerChange) {
        this.$emit('change', val, name);
      } else {
        this.$emit('input', val, name);
      }
    },
    setCurrentDictOptions(dictOptions) {
      this.dictOptions = dictOptions
    },
    getCurrentDictOptions() {
      return this.dictOptions
    }
  }
}
</script>

<style scoped>
</style>
